package com.iatinspur.listener;

import com.iatinspur.context.UserContext;
import com.iatinspur.listener.event.MyTestEvent;
import org.slf4j.Logger;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

/**
 * 监听器 测试
 * 1.添加异步处理 @EnableAsync 开启异步(必须有)
 * 2.@Async("myThread") 这里要是没有设置线程池 那么用spring自带
 * 3.选择由使用类 传日志对象进行处理
 */
@Component
//@Async("myThread")
public class MyTestListener  implements ApplicationListener<MyTestEvent>{
    @Override
    public void onApplicationEvent(MyTestEvent event) {

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        Logger log = event.getLog();
        log.info("MyTestListener说{}",event.getMsg());
        log.info("MyTestListener说{},当前线程{}",event.getMsg(),Thread.currentThread().getName());

        log.info("MyTestListener:【{}】", UserContext.get());
    }
}
